//
//  IcRecommendationsListViewController.m
//  IceClear
//
//  Created by P & C on 4/7/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "IcRecommendationsListViewController.h"
#import "IcRecommendationDetailViewController.h"


@implementation IcRecommendationsListViewController
@synthesize appModelsManager;
@synthesize recommendationArray;

#pragma mark -
#pragma mark View lifecycle

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];


	if (self.navigationItem) {
		// init navigation controls
		
		UIBarButtonItem* btnAdd = [[UIBarButtonItem alloc] 
								   initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self 
								   action:@selector(onShowRequestRecommendationAction:)];
		
		self.navigationItem.rightBarButtonItem = btnAdd;
		
		[btnAdd release];
	}
	
	self.view.backgroundColor = [UIColor clearColor];
}

-(void)onShowRequestRecommendationAction:(id)sender {
	if (self.navigationController) {
		// create view controller to get a new recommendation and push it
		
		IcRequestRecommendationViewController* vc = [[IcRequestRecommendationViewController alloc] 
													 initWithNibName:@"IcRequestRecommendationViewController" bundle:nil];
		
		vc.title = @"Request Recommendation";
		vc.appModelsManager = self.appModelsManager;
		
		[self.navigationController pushViewController:vc animated:YES];
		[vc release];
	}
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
	[self.tableView reloadData];
}

/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/


#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [recommendationArray count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
    if ([recommendationArray count] > 0) {
		IcRecommendation* recommendation = (IcRecommendation *)[recommendationArray objectAtIndex:indexPath.row] ;
		//NSLog(@" Recommendation.name Class = %@",[recommendation.name class]);
		// Configure the cell...
		NSString* text = recommendation.name;
		
		cell.textLabel.text = text;
		cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
		//cell.detailTextLabel.text = material.details;
		//cell.detailTextLabel.font = [UIFont systemFontOfSize:12];
	}
	else {
		cell.textLabel.text = @"No Recommendations Found!";
		//cell.detailTextLabel.text = @"";
	}
    
    return cell;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source.
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }   
}
*/


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
    
	IcRecommendation* recommendation = (IcRecommendation *)[recommendationArray objectAtIndex:indexPath.row];
	
	IcRecommendationDetailViewController* detailVc = [[IcRecommendationDetailViewController alloc] 
													  initWithStyle:UITableViewStyleGrouped];
	
	// customize it first and give it references to the models it
	// needs in order to function
	detailVc.title = recommendation.name;
	detailVc.recommendation = recommendation;
	detailVc.appModelsManager = appModelsManager;
	
	[self.navigationController pushViewController:detailVc animated:YES];
    [detailVc release];	
}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


- (void)dealloc {
	[appModelsManager release];
	[recommendationArray release];
    [super dealloc];
}


@end

